from blog import db
from flask_login import UserMixin

class User(db.Model, UserMixin):
    __tablename__ = 'b_user'
    id = db.Column(db.Integer,primary_key=True)
    username = db.Column(db.String(10),unique=True)
    password = db.Column(db.String(16))

    def __init__(self,id,username,password):
        self.id = id
        self.username = username
        self.password = password

    @property
    def __repr__(self):
        return '<User %r>' % self.username

    @property
    def is_active(self):
        return True

    #注意！这里必须重写，因为源码使用unicode(id)，但是python3没有unicode()方法！此时就无法登录用户！
    def get_id(self):
        return self.id

    @staticmethod
    def get(user_id):
        """try to return user_id corresponding User object.
        This method is used by load_user callback function
        """
        if not user_id:
            return None
        try:
            if user_id == 1 :  #最好从文件或数据库读取id（这里为简单写死为1了）
                return User()
        except:
            return None


    @property
    def is_authenticated(self):
        return True
